import { useTableData } from "@/components/SmTable/src/utils";
import { mockFieldMap, mockList } from "@/utils/mock";
import { mockArr } from "./mock";
import { bankAccountTypeMap, enableMap, isNotMap, itemTypeMap } from "@/commonData/dict";
import SalespersonsAPI from "@/api/sales-manage/salespersons";

export const useTable = () => {
  const list: any = ref([]);
  const total = ref(0);
  const loading = ref(false);

  const { tableOptions, transformTableOptions } = useTableData();

  const fieldMap = {
    bankAccountName: "账户名称",

    bankAccountNumber: "银行账号",
    bankAccountType: {
      label: "银行账户类型",
      slot: "bankAccountType",
      textMap: bankAccountTypeMap,
    },
    bankName: "银行名称",

    status: {
      label: "状态",
      slot: "status",
      textMap: enableMap,
    },

    costAdjustmentRate: "动态补偿率",
    freezingCommission: "锁定佣金",
    genderText: "性别",

    idCardNo: "身份证号",
    mobile: "手机号",
    name: "姓名",

    orgNo: "机构编号",
    remainingCommission: "剩余佣金",
    username: "用户名",
    totalCommission: "累计佣金",
    totalPerformance: "总业绩",
    remark: "备注",

    createdAt: "创建时间",

    updatedAt: "更新时间",
    operate: {
      label: "操作",
      slot: "operate",
      fixed: "right",
      width: "180px",
    },
  };

  // const fieldMap = mockFieldMap(mockArr);

  tableOptions.value = transformTableOptions(fieldMap);

  // // 先模拟数据
  // const getList = async () => {
  //   const data = mockList(mockArr);
  //   list.value = data.list;
  //   total.value = data.total;
  // };

  const queryParams: any = reactive({
    pageNum: 1,
    pageSize: 10,
  });

  /** 查询列表 */
  const getList = async () => {
    loading.value = true;
    try {
      const data = await SalespersonsAPI.getPage(queryParams);
      list.value = data.list;
      total.value = data.total;
    } finally {
      loading.value = false;
    }
  };

  function handleQuery() {
    queryParams.pageNum = 1;
    getList();
  }

  const queryFormRef = ref();
  /** 添加/修改操作 */
  const formRef = ref();
  const openForm = (type: string, id?: number) => {
    formRef.value.open(type, id);
  };

  /** 重置按钮操作 */
  const handleResetQuery = () => {
    queryFormRef.value.resetFields();
    nextTick(() => {
      handleQuery();
    });
  };

  return {
    tableOptions,
    list,
    total,
    loading,
    queryParams,
    getList,
    handleQuery,
    handleResetQuery,
    queryFormRef,
    formRef,
    openForm,
  };
};
